<?php

include '../templateSys/SQL.php';
include '../templateSys/Settings.php';
include '../templateSys/manager_util.php';
include '../templateSys/Admin.php';

//loginCheck
checkUserFinal();

global $link;
$link = getLink();

if (isset($_POST['newSectionRelation']) && isset($_POST['SectionName'])) {
    $name = generateName();
    insertText($name, $_POST['SectionName'], Settings::$systemAdminId);
    
    /*$query = sprintf(SQL::$insert_text_preview, $name, mysql_real_escape_string($_POST['SectionName']), mysql_real_escape_string(Settings::$systemAdminId)
    );
    $result = mysql_query($query, $link);*/


    $query = sprintf(SQL::$insert_itemType_empty,$name, $name
    );
    $result = mq($query, $link);
    $id = mysql_insert_id();

    if (isset($_POST["SectionParent"]) && $_POST["SectionParent"] != "") {
        $query = sprintf(SQL::$select_itemType_byName, mysql_real_escape_string($_POST["SectionParent"])
        );
        $result = mq($query, $link);
        $row = mysql_fetch_assoc($result);
        if ($row) {
            $mainMenuId = $row['id'];
        }        
    } else {
        $query = sprintf(SQL::$select_itemType_byName, mysql_real_escape_string(Settings::$mainMenu)
        );
        $result = mq($query, $link);
        $row = mysql_fetch_assoc($result);
        if ($row) {
            $mainMenuId = $row['id'];
        }
    }

    $query = sprintf(SQL::$insert_subtypeMapping,  mysql_real_escape_string($mainMenuId),mysql_real_escape_string($id)
    );
    $result = mq($query, $link);
    mysql_close($link);
    header("location: " . Settings::$rootPath . Settings::$manager . Settings::$success);
    mysql_close($link);
    exit();
}

if (isset($_POST['pridaniBarvy'])) {
    addCiselnik(1, $_POST['barva']);
    $id = mysql_insert_id();
    $query = sprintf(SQL::$select_ciselnik_velikosti);    
    $result = mq($query, $GLOBALS["link"]);
    while ($row = mysql_fetch_array($result)) {
        addCiselnik(3, $id."_".$row["idval"]);
    }    
    header("location: " . Settings::$rootPath . Settings::$manager . Settings::$success);
    mysql_close($link);
    exit();
}

if (isset($_POST['pridaniVelikosti'])) {
    if (isset($_POST['sortNumber'])){
        addCiselnik(2, $_POST['velikost'], $_POST['sortNumber']);
    }else{
        addCiselnik(2, $_POST['velikost']);
    }
    
    $id = mysql_insert_id();
    $query = sprintf(SQL::$select_ciselnik_barvy);    
    $result = mq($query, $GLOBALS["link"]);
    while ($row = mysql_fetch_array($result)) {
        addCiselnik(3, $row["idval"]."_".$id);
    }    
    header("location: " . Settings::$rootPath . Settings::$manager . Settings::$success);
    mysql_close($link);
    exit();
}

if (isset($_POST['pridaniZnacky'])){
    $znackaImage = generateName();
    uploadFile("znackaImage", $znackaImage);
    
    $znackaJmeno = generateName();
    $znackaJmenoData = $_POST['znackaJmeno'];    
    createText($znackaJmeno, $znackaJmenoData, getUser()->getAdminId());
    
    $fkname = "$znackaImage;$znackaJmeno";
    $idcismap = 0;
    $idcis = generateInt();
    insertDynCisMapFK($idcismap, $idcis, $fkname);
    
    header("location: " . Settings::$rootPath . Settings::$manager . Settings::$success);
    mysql_close($link);
    exit();
}

function addCiselnik($id,$value,$sortNumber = 0){
    $query = sprintf(SQL::$insert_ciselnik, $id, $value, $sortNumber);
    $result = mq($query, $GLOBALS["link"]);
}

mysql_close($link);
?>
